package com.hlingsoft.kkh.action.admin;


import com.hlingsoft.kkh.entity.ReportRs;
import com.hlingsoft.kkh.entity.vo.JsonVo;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by selbylei on 2015/6/23.
 */
@Controller
@RequestMapping("admin/reportRs")
public class AdminReportRsAction extends AdminBaseAction {


    /**
     * 列表页
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/list.htm")
    public String list(
            HttpServletRequest request,
            ModelMap modelMap) {
        List<ReportRs> reportRsList = reportRsDao.getReportRs();
        modelMap.put("reportRsList", reportRsList);
        return "system/reportRs/list";
    }




    /**
     * 添加
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/add.htm")
    public String add(HttpServletRequest request, ModelMap modelMap) {
        return "system/reportRs/add";
    }




    /**
     * 添加兴趣json
     * @param name
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/add.json")
    @ResponseBody
    public JsonVo<String> add(
            @RequestParam(value = "name") String name,
            ModelMap modelMap) {
        JsonVo<String> json = new JsonVo<String>();
        try {

            json.setResult(true);
            ReportRs reportRs = new ReportRs();
            reportRs.setName(name);
            reportRsDao.addReportRs(reportRs);
            return json;
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg("输入参数有误");
            json.setResult(false);
            return json;
        }
    }

    /**
     * 禁用
     * @param reportRsId
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/takeOff.json")
    @ResponseBody
    public JsonVo<String> delete(@RequestParam(value = "reportRsId") long reportRsId,
                                 ModelMap modelMap) {
        JsonVo<String> json = new JsonVo<String>();
        ReportRs reportRs = reportRsDao.getReportRsById(reportRsId);
        reportRs.setAvailable(0);
        reportRsDao.updateReportRs(reportRs);
        json.setResult(true);
        return json;
    }


    /**
     * 编辑
     * @param reportRsId
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/edit.htm")
    public String detail(@RequestParam(value = "reportRsId") long reportRsId,
                         HttpServletRequest request, ModelMap modelMap) {
        modelMap.put("reportRs", reportRsDao.getReportRsById(reportRsId));
        return "system/reportRs/edit";
    }


    @RequestMapping(value = "/edit.json")
    @ResponseBody
    public JsonVo<String> edit(
            @RequestParam(value = "reportRsId") long reportRsId,
            @RequestParam(value = "name",required = false) String name,
            ModelMap modelMap) {
        JsonVo<String> json = new JsonVo<String>();
        try {
            json.setResult(true);
            ReportRs reportRs = reportRsDao.getReportRsById(reportRsId);
            reportRs.setName(name);
            reportRsDao.updateReportRs(reportRs);
            return json;
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg("输入参数有误");
            json.setResult(false);
            return json;
        }
    }
}
